package com.magist.manage.web.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.CxtHolder;
import com.magist.core.entity.ResultMessage;
import com.magist.core.enums.UserTypeEnum;
import com.magist.core.exception.BusinessException;
import com.magist.manage.web.dto.BusinessLogDto;
import com.magist.manage.web.entity.BusinessLog;
import com.magist.manage.web.entity.UserInfo;
import com.magist.manage.web.service.BusinessLogService;
import com.magist.manage.web.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(value="业务日志接口",tags={"业务日志接口"})
@RestController
@RequestMapping("/rest/businessLog")
public class RestBusinessLogController extends BaseController {

	@Autowired
	private BusinessLogService businessLogService;
	@Autowired
	private UserInfoService userInfoService;


	@ApiOperation(value="保存", notes="保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("businessLogDto") BusinessLogDto businessLogDto) {
		BusinessLog businessLog = new BusinessLog();
		BeanUtils.copyProperties(businessLogDto,businessLog);
		businessLog.setId(businessLogService.getNewId());
		businessLogService.insert(businessLog);
		return ResultMessage.success();
	}

	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("businessLogDto") BusinessLogDto businessLogDto) {
		BusinessLog oldBusinessLog = businessLogService.getById(businessLogDto.getId());
		if(oldBusinessLog==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		BusinessLog businessLog = new BusinessLog();
		BeanUtils.copyProperties(businessLogDto,businessLog);
		businessLog.setModifyTime(oldBusinessLog.getModifyTime());
		if (businessLogService.update(businessLog)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("businessLogDto") BusinessLogDto businessLogDto) {
		ResultMessage result = ResultMessage.success();
		BusinessLog businessLog = new BusinessLog();
		BeanUtils.copyProperties(businessLogDto,businessLog);
		if(!StringUtils.isEmpty(businessLog.getUserName())) {
			businessLog.setUserName("%" + businessLog.getUserName()+"%");
		}

		UserInfo userInfo = userInfoService.getById(CxtHolder.getUser().getUserId());
		if (UserTypeEnum.OP.getCode().equals(userInfo.getUserType())) {
			businessLog.setCreateById(userInfo.getId());
		}
		PageInfo<BusinessLog> pageInfo = businessLogService.page(businessLog,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		BusinessLog businessLog = businessLogService.getById(id);
		if(businessLog==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(businessLogService.delete(businessLog)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		BusinessLog businessLog = businessLogService.getById(id);
		if(businessLog==null)
			throw new BusinessException("无效的数据!");
		BusinessLogDto businessLogDto = new BusinessLogDto();
		BeanUtils.copyProperties(businessLog,businessLogDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(businessLogDto);
		return result;
	}
}
